package common.match
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.match.MatchDataManager;
	import com.liangzhong.model.type.MatchType;
	import com.lz.football.module.match.view.IMatchPanel;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.core.TitleWindow;

	/**
	 *比赛窗口 
	 * @author lake
	 * 
	 */	
	public class MatchPanel extends TitleWindow implements IMatchPanel
	{
		/**
		 *最小化后条目 
		 */		
		private var matchSmallItem 	:MatchSmallItem;
		/**
		 *比赛视图 
		 */		
		private var matchView 			:*;
		/**
		 *数据 
		 */		
		protected var _data			:*;
		
		public function MatchPanel()
		{
			super();
		}
		/**
		 *获取设置视图 
		 * @param value
		 * 
		 */		
		public function set view(value :*):void
		{
			matchView = value;
			_symbolSkin.addChild(value as DisplayObject);
			value.y = 38;
			value.x = 4;
		}
		public function get view():*
		{
			return matchView;
		}
		/**
		 *获取设置数据 
		 * @return 
		 * 
		 */		
		public function get data():*
		{
			return _data;
		}
		public function set data(value:*):void
		{
			_data = value;
			//比赛类型
			var mtype :int = int(_data.type);
			_symbolSkin.nameMc.gotoAndStop(mtype);
			_symbolSkin.nameMc.mouseChildren = false;
			_symbolSkin.nameMc.mouseEnabled = false;
		}
		/**
		 *初始化 
		 * 
		 */	
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("common.match.MatchPanelUISkin");
			this.addChild(_symbolSkin);
			//初始化宽高
			width = _symbolSkin.width;
			height = _symbolSkin.height;
			//初始化UI界面控件
			windowBg = _symbolSkin.windowBg;//背景框
			_symbolSkin.bg.mouseChildren = false;
			_symbolSkin.bg.mouseEnabled = false;
			_symbolSkin.bg.cacheAsBitmap = true;
			//设置可拖动
			dragEnabled = true;
			//自适应
			resizeEnabled = true;
			//设置关闭按钮
			setCloseBtn(_symbolSkin.closeBtn);
			
			addEvtListener();
		}
		/**
		 *注册事件 
		 * 
		 */	
		public function addEvtListener():void
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownH);
		}
		/**
		 *自适应 
		 * @param e
		 * 
		 */		
		override protected function onResizeHandler(e :Event):void
		{
			if(matchSmallItem)
			{
				matchSmallItem.x = (GlobalDef.GameStage.stage.stageWidth - matchSmallItem.width) - 10;
				matchSmallItem.y = (GlobalDef.GameStage.stage.stageHeight - matchSmallItem.height) - 130;
			}
		}
		/**
		 * 关闭按钮响应
		 * @param e
		 * 
		 */		
		override protected function onCloseHandler(e :MouseEvent):void
		{
			var count :int = MatchDataManager.SmallCount;
			matchSmallItem = new MatchSmallItem();
			matchSmallItem.initUI();
			GlobalDef.GameStage.addChild(matchSmallItem);
			matchSmallItem.x = (GlobalDef.GameStage.stage.stageWidth - matchSmallItem.width) - 10;
			matchSmallItem.y = (GlobalDef.GameStage.stage.stageHeight - matchSmallItem.height) - count * (matchSmallItem.height + 10) - 130;
			matchSmallItem.data = {view:this,name:_data.name,type:_data.type};
			matchSmallItem.setScore(0,0);
			MatchDataManager.SmallItemS.push(matchSmallItem);
			MatchDataManager.SmallCount ++;
			if(matchView)
			{
				matchView.setSmallScore();
			}
		}
		/**
		 *设置比分 
		 * @param selfScore
		 * @param opeScore
		 * 
		 */		
		public function setScore(selfScore:int,opeScore:int):void
		{
			if(matchSmallItem)
			{
				matchSmallItem.setScore(selfScore,opeScore);
			}
		}
		/**
		 *设置时间 
		 * @param value
		 * 
		 */		
		public function setTime(value :String):void
		{
			if(matchSmallItem)
			{
				matchSmallItem.setTime(value);
			}
		}
		/**
		 *鼠标按下 
		 * @param e
		 * 
		 */		
		private function onMouseDownH(e :MouseEvent):void
		{
			if(this.parent)
			{
				this.parent.setChildIndex(this,this.parent.numChildren - 1);
			}
		}
		/**
		 *删除事件 
		 * 
		 */	
		public function removeEvtListener():void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownH);
		}
		/**
		 * 删除事件
		 * @param e
		 * 
		 */		
		override protected function removeEvent(e:Event = null):void
		{
			
		}
		/**
		 *销毁 
		 * 
		 */	
		public function destroyView():void
		{
			removeEvtListener();
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			matchSmallItem = null;
			matchView = null;
			super.removeEvent();
		}
	}
}